//
//  DXVisitView.swift
//  weiBoOfSwift
//
//  Created by simon on 15/12/6.
//  Copyright © 2015年 simon. All rights reserved.
//

import UIKit
import SnapKit
// MARK: 协议
@objc protocol DXVisitViewDelegate:NSObjectProtocol{

//    注册
   
  optional func visitViewRegisterBtnSelected()
//    登录
   optional func visitViewLoginBtnSelected()

}
class DXVisitView: UIView,UITableViewDelegate{
    
    let KintroLablWith = 260
    let kregisterBtnWith = 120
    let KintroLabelTopMargin = 0
    let KregisterBtnTopMargin = -10
    
    weak var delegate:DXVisitViewDelegate?
  
    
    // MARK: - 懒加载
    lazy var houseView:UIImageView = {
        let houseView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        houseView.sizeToFit()
        
        return houseView
    }()
    // - 懒加载circleView
    lazy var circleView:UIImageView = {
        let circleView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        circleView.sizeToFit()
        return circleView
    }()
    
    //  懒加载introLabel
    lazy var introLabel:UILabel = {
        let introLabel = UILabel()
        introLabel.text = "关注一些人,回这里看看有什么惊喜关注一些人,回这里看看有什么惊喜"
        introLabel.textAlignment = .Center
        introLabel.numberOfLines = 0
        
        return introLabel
    }()
    
    // 懒加载registerBtn
    lazy var registerBtn:UIButton = {
        let registerBtn = UIButton()
        registerBtn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
        registerBtn.setTitle("注册", forState: .Normal)
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        registerBtn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return registerBtn
    }()
    //  懒加载loginBtn
    lazy var loginBtn:UIButton = {
        let loginBtn = UIButton()
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        loginBtn.setTitle("登陆", forState: .Normal)
        loginBtn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        loginBtn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return loginBtn
    }()
    
    lazy var shadowView:UIImageView = {
        let shadowView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return shadowView
        
    }()
    
    // MARK: - visitView初始化方法
    override init(frame: CGRect) {
        super.init(frame:frame)
       
        addSubview(circleView)
        addSubview(shadowView)
        addSubview(introLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        addSubview(houseView)
        startCircleAnimation()
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 外界修改visitView内部子控件的方法
    func changSubViews(introLabelString introLabelString:String,iconName:String){
        introLabel.text = introLabelString
        houseView.image = UIImage(named: iconName)
        circleView.hidden = true
    }

    // MARK: - 让圈转起来
    @objc private func startCircleAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        
        anim.repeatCount = MAXFLOAT
        
        anim.duration = 20
//        完成后是否移除动画
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: "circleAnimation")

    }
    
    // MARK: - 登录和注册按钮点击事件
    @objc private func registerBtnClick(){
        delegate?.visitViewRegisterBtnSelected?()
    }
    
    // MARK: - loginBtnClick,网络请求数据
    @objc private func loginBtnClick(){
        delegate?.visitViewLoginBtnSelected?()
//        let urlString = "http://localhost/topic_news.json"
//    DXNetWorkClient.requestNetWork(.GET, URLStirng: urlString, parameters: nil, success: { (responser) -> () in
//        logPrint(responser)
//        }) { (error) -> () in
//        logPrint(error)
//        }

    }
    
    
    // MARK: - 布局子控件
    override func layoutSubviews() {
        super.layoutSubviews()
      
        layoutMyViews()
    }
    
    // MARK: - 给子控件添加约束
     private func layoutMyViews(){
//        houseView的约束
        houseView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
//        circleView的约束
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(houseView.snp_center)
        }
       
//       introLabel的约束
        introLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(circleView.snp_bottom).inset(KintroLabelTopMargin)
            make.width.equalTo(KintroLablWith)
            make.centerX.equalTo(circleView.snp_centerX)
        }
//        registerBtn的约束
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(introLabel.snp_leading)
            make.top.equalTo(introLabel.snp_bottom).inset(KregisterBtnTopMargin)
            make.width.equalTo(kregisterBtnWith)
        }
//        loginBtn的约束
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(introLabel.snp_trailing)
            make.width.equalTo(registerBtn.snp_width)
            make.top.equalTo(registerBtn.snp_top)
        }
//        shadowView的约束
        shadowView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.width.equalTo(self.snp_width)
            make.bottom.equalTo(loginBtn.snp_bottom)
        }
 

        
    }
    // MARK: - 原生约束
     private func layoutMyViews2(){
    
        //        houseView的约束
        houseView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item:houseView, attribute: .CenterX, relatedBy:.Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item:houseView, attribute: .CenterY, relatedBy:.Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        //        circleView的约束
        circleView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: houseView, attribute:.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: houseView, attribute:.CenterY, multiplier: 1, constant: 0))
        //       introLabel的约束
        introLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: introLabel, attribute: .Top, relatedBy: .Equal, toItem: houseView, attribute:.Bottom, multiplier: 1, constant: 60))
        
        addConstraint(NSLayoutConstraint(item: introLabel, attribute: .CenterX, relatedBy: .Equal, toItem: houseView, attribute:.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: introLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute:.NotAnAttribute, multiplier: 1, constant: 260))
        //        registerBtn的约束
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Leading, relatedBy: .Equal, toItem: introLabel, attribute:.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: introLabel, attribute:.Bottom, multiplier: 1, constant: 40))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute:.NotAnAttribute, multiplier: 1, constant: 100))
        //        loginBtn的约束
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Trailing, relatedBy: .Equal, toItem: introLabel, attribute:.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: registerBtn, attribute:.Top, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: registerBtn, attribute:.Width, multiplier: 1, constant: 0))
        
        //        shadowView的约束
        shadowView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: shadowView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute:.Top, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: shadowView, attribute: .Bottom, relatedBy: .Equal, toItem: introLabel, attribute:.Top, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: shadowView, attribute: .Width, relatedBy: .Equal, toItem: self, attribute:.Width, multiplier: 1, constant: 0))

    }
    
}
